/**
    ******************************************************************************
    * @file    sqlconnection.h
    * @author  debian
    * @version V1.0.0
    * @date    2020-03-08
    * @brief   sql链接程序
    ******************************************************************************
    * @attention
    *
    *
    ******************************************************************************
    */ 

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __SQLCONNECTION_H
#define __SQLCONNECTION_H

#ifdef __cplusplus
    extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "mysql.h"
#include <stdio.h>
#include <assert.h>

/** @addtogroup Sqlconnection
    * @{
    */
class SqlResult
{
    public:
        SqlResult() : _sqlres(NULL)
        {}

        ~SqlResult()
        {
            mysql_free_result(_sqlres);
        }

        inline void SerResult(MYSQL_RES* pRes)
        {
            assert(_sqlres == NULL);
            if(_sqlres) {
                printf("MYSQL_RES已经存储了结果，可能会导致内存泄漏");
            }

            _sqlres = pRes;
        }

        inline MYSQL_RES* GetResult()
        {
            return _sqlres;
        }

        void FetchRow(MYSQL_ROW& row)
        {
            row = mysql_fetch_row(_sqlres);
        }

        int RowCount()
        {
            return  _sqlres->field_count; 
        }

        inline int GetRowCount()  //这个表有多少条数据
        {
            return _sqlres->row_count;
        }

    private:
        MYSQL_RES* _sqlres;
};


class Sqlconnection
{
    public:
        Sqlconnection();
        ~Sqlconnection();

        bool connect(const char *host, const char *user, const char *passwd, const char *db, unsigned int port);
        const char* GetErrInfo();
        bool Execyte(const char* Sql);
        bool Execyte(const char* sql, SqlResult& res);
        void Reconnect();
        void Close();

        bool CreatDataBase(const char* name);
        bool UseDataBase(const char* name);
        MYSQL_ROW GetData(const char* tablename, const char* key, const char* value);

        bool Transaction(char** sqls, int size);
    private:
        MYSQL *_mysql;

    protected:

};


/* Exported types ------------------------------------------------------------*/
/** @defgroup 
    * @{
    */ 



/**
    * @}
    */


/* Exported constants --------------------------------------------------------*/

/* Exported macro ------------------------------------------------------------*/

/* Exported functions --------------------------------------------------------*/ 


/**
    * @}
    */

#ifdef __cplusplus
}
#endif

#endif /* __SQLCONNECTION_H */

/******************* (C) 2020 HOME 深圳龙华 *****END OF FILE****/

